<?php
require_once("../inc/function.php");

function GetCompanies()
{
	$conn = GetConnection();
	return $conn->query('SELECT * FROM Companies');
}

function GetCompany($id)
{
	$conn = GetConnection();
	if($id == null)	
		die("No company was found with the prodived id or the id is invalid");	
	$results = $conn->query("SELECT * FROM Companies WHERE id='$id'");
    $row = $results->fetch_assoc();
    $conn->close();
    return $row;
}

function SaveCompany($row)
{
	$conn = GetConnection();
    $name = strip_tags($row['name']);
    $id = $row['id'];
    if((strlen($name) > 0))
    {
    	$conn->query("UPDATE Companies SET name='$name' WHERE id='$id'");
    }
    else
        header("Location: http://cs.newpaltz.edu/~N010852322/Final/Companies/edit.php?id=".$id."");
        
        $conn->close();
}

function NewCompany($row)
{
	$name = strip_tags($row['name']);
	if((strlen($name) > 0))
	{
    	$conn = GetConnection();
        if($conn->query("INSERT INTO Companies (id,name) VALUES (NULL,'$name')"))
        {
        	$conn->close();
        }
        else
        {
        	die("Query failed");
        }
    }
    else 
    {
    	die("You need to enter a Company name.");
    }
}

function DeleteCompany($id)
{
	$conn = GetConnection();
    $results = $conn->query("Delete FROM Companies WHERE id=$id");
    header('Location: http://cs.newpaltz.edu/~N01852322/Final/Companies/');
}
?>